# Grid class:
#__init__ -> creates empty table with spaces in slots
#Move -> Takes a move and inserts it into table; double-checks to make sure that it is either X or O
#__repr__ -> Prints out table



from Exc import *


class Grid:
    
    #Upon creation, make empty table
    def __init__(self):
        self.Table=[[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
        self.Human='X'
        self.Comp='O'
        self.Diff=4






    #Make move; remember indexing starts at zero!!!
    def Move(self, Person, y, x):
        if self.Table[y][x] == ' ':
                self.Table[y][x]=Person

        else:
            raise IllegalMove
            print 'That move is not possible: Move already there'






    #Print out table
    def Display(self):
        print '%s|%s|%s' % (self.Table[0][0], self.Table[0][1], self.Table[0][2])
        print '------'
        print '%s|%s|%s' % (self.Table[1][0], self.Table[1][1], self.Table[1][2])
        print '------'
        print '%s|%s|%s' % (self.Table[2][0], self.Table[2][1], self.Table[2][2])






    #See if someone has won
    def CheckWin(self):
        
        XRow=0
        ORow=0
        XCol=0
        OCol=0
        #Upper-left to lower-right diagonal counters
        XDiag1=0
        ODiag1=0
        #Upper-right to lower-left diagonal counters
        XDiag2=0
        ODiag2=0
        
        for pos1 in range(3):

            ###print 'pos1=%s' % (pos1)

            #Zero out row and column counters
            XRow=0
            ORow=0
            XCol=0
            OCol=0

            #Check Diagonals (upper-left to lower-right)
            if self.Table[pos1][pos1] == 'X':
                XDiag1 = XDiag1+1
                
                if XDiag1 == 3:
                    print 'X wins in the upper-left diagonal'
                    raise Winner

            elif self.Table[pos1][pos1] == 'O':
                ODiag1 = ODiag1+1
                
                if ODiag1 == 3:
                    print 'O wins in the upper-left diagonal'
                    raise Winner




            #Check Diagonals (upper-right to lower-left)
            if self.Table[pos1][2-pos1] == 'X':
                XDiag2 = XDiag2+1
                
                if XDiag2 == 3:
                    print 'X wins in the upper-right diagonal'
                    raise Winner

            elif self.Table[pos1][2-pos1] == 'O':
                ODiag2 = ODiag2+1
                
                if ODiag2 == 3:
                    print 'O wins in the upper-right diagonal'
                    raise Winner



                    


            for pos2 in range(3):

                ###print 'pos2=%s' % (pos2)

                #Check Rows
                if self.Table[pos1][pos2] == 'X':
                    XRow = XRow+1
                    
                    if XRow == 3:
                        print 'X wins in row %i' % (pos1)
                        raise Winner
                    
                elif self.Table[pos1][pos2] == 'O':
                    ORow = ORow+1
                    
                    if ORow == 3:
                        print 'O wins in row %i' % (pos1)
                        raise Winner

                #Check Columns
                #for some unknown reason, this must be an IF; changing to ELIF prevents it from workin
                if self.Table[pos2][pos1] == 'X':
                    XCol = XCol+1
                    
                    if XCol == 3:
                        print 'X wins in column %i' % (pos1)
                        raise Winner
                    
                elif self.Table[pos2][pos1] == 'O':
                    OCol = OCol+1
                    
                    if OCol == 3:
                        print 'O wins in column %i' % (pos1)
                        raise Winner


                    
                    





    #Check to see if it is a Cat's Game
    def CatsGame(self):
        #Set initial value so that we assume it is a Cat's Game
        Continue=0

        #Reiterate through each column
        for x in range(3):

            #Reiterate through each row if an open slot has not been found 
            if Continue == 0:
                for y in range(3):
                
                    if self.Table[y][x] == ' ':
                        Continue=1

        #If no open slot is found, raise exception
        if Continue == 0:
            raise CatsGame
